package com.mytijian.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.mytijian.exception.BizException;
import com.mytijian.exception.CommonExceptionEnum;

/**
 * Created by Administrator on 2017/8/17.
 */
public class DateUtils {
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String YYYYMMDD = "yyyyMMdd";
	public static final String YYYY_MM_DD_HMS = "yyyy-MM-dd HH:mm:ss";
	public static final String YYYYMMDDSS = "yyyyMMddHHmmss";

	private DateUtils() {
	}

	public static int daysBetween(Date startDate, Date endDate) {
		return (int) ((endDate.getTime() - startDate.getTime()) / 86400000L);
	}

	public static String format(String format, Date date) {
		if (date == null) {
			return null;
		} else {
			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
			return dateFormat.format(date);
		}
	}

	public static String format(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}

	public static Date parse(String format, String dateStr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		try {
			return dateFormat.parse(dateStr);
		} catch (ParseException ex) {
			throw new BizException(CommonExceptionEnum.DATE_PARSE_ERROR);
		}
	}

	public static Date parse(String dateStr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return dateFormat.parse(dateStr);
		} catch (ParseException ex) {
			throw new BizException(CommonExceptionEnum.DATE_PARSE_ERROR);
		}
	}

}
